/*
 * @(#)Options.java        0.1 09/04/29
 * @version 	0.1 29 Apr 2009
 * @author 	Tran Xuan Thanh
 * 
 * The hien cac tuy chon cua chuong trinh, cho phep nguoi dung thay doi cac tuy chon
 */



package GUI;

import Data.*;
import javax.swing.*;
import java.util.*;
import java.awt.*;
//import XML_IO.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class Options extends JDialog implements ActionListener {
	private static Options instance = new Options();
	public static Options getInstance() {
		return instance;
	}
	protected Options() {
		options = new XML_IO.Options();
		themes = options.getThemes();
		languages = options.getLanguages();
	}
	
	XML_IO.Options options;
	ResourceBundle lang;
	private static LinkedList<Theme> themes;
	private static LinkedList<Language> languages;
	
	/*
	 * Lay cac chuoi text trong ngon ngu dang su dung
	 */
	static ResourceBundle getLanguage() {
		Language tmp = getCurrentLanguage();
		Locale locale = new Locale(tmp.getBase(), tmp.getLocale());
		return ResourceBundle.getBundle("lang", locale);
	}
	
	/*
	 * Lay theme dang su dung
	 */
	public static Theme getCurrentTheme() {
		int size = themes.size();
		for (int i = 0; i < size; i++) {
			if (themes.get(i).isCurrent()) return themes.get(i);
		}
		return themes.get(0);
	}
	
	public static Language getCurrentLanguage() {
		int size = languages.size();
		for (int i = 0; i < size; i++) {
			if (languages.get(i).isCurrent()) return languages.get(i);
		}
		return languages.get(0);
	}

	private JDialog optionDialog;
	private JButton OKButton;
	private JButton cancelButton;
	private JComboBox languagesComboBox;
	private JComboBox themesComboBox;
	
	/*
	 * Hien hop thoai tuy chon
	 */
	public void show(JFrame f) {
		lang = getLanguage();
		if (optionDialog != null) {
			optionDialog.setVisible(true);
			return;
		}
		optionDialog = new JDialog(f);
		
		optionDialog.setSize(400, 300);
		Toolkit toolkit = Toolkit.getDefaultToolkit();
		Dimension screenSize = toolkit.getScreenSize();
		int x = (screenSize.width - optionDialog.getWidth()) / 2;
		int y = (screenSize.height - optionDialog.getHeight()) / 2;
		optionDialog.setLocation(x, y);
		optionDialog.setVisible(true);
		
		optionDialog.setTitle(lang.getString("OptionDialog.text"));
		optionDialog.setLayout(new GridLayout(3, 1));
		
		JPanel languagePanel = new JPanel();
		languagePanel.setBorder(BorderFactory.createTitledBorder(lang.getString("OptionDialog.languagePanel.text")));
		optionDialog.add(languagePanel);
		JLabel languageLabel = new JLabel(lang.getString("OptionDialog.languageLabel.text"));
		languagePanel.add(languageLabel);
		
		// Tuy chon ngon ngu
		languagesComboBox = new JComboBox();
		languagePanel.add(languagesComboBox);
		int size = languages.size();
		for (int i = 0; i < size; i++) {
			languagesComboBox.addItem(languages.get(i).getName());
		}
		languagesComboBox.setSelectedItem(getCurrentLanguage().getName());
		
		JPanel themePanel = new JPanel();
		themePanel.setBorder(BorderFactory.createTitledBorder(lang.getString("OptionDialog.themePanel.text")));
		optionDialog.add(themePanel);
		JLabel themeLabel = new JLabel(lang.getString("OptionDialog.themeLabel.text"));
		themePanel.add(themeLabel);
		
		// Tuy chon theme
		themesComboBox = new JComboBox();
		themePanel.add(themesComboBox);
		size = themes.size();
		for (int i = 0; i < size; i++) {
			themesComboBox.addItem(themes.get(i).getName());
		}
		themesComboBox.setSelectedItem(getCurrentTheme().getName());
		
		JPanel buttonPanel = new JPanel();
		optionDialog.add(buttonPanel);
		OKButton = new JButton(lang.getString("OptionDialog.OKButton.text"));
		buttonPanel.add(OKButton);
		OKButton.addActionListener(this);
		cancelButton = new JButton(lang.getString("OptionDialog.cancelButton.text"));
		buttonPanel.add(cancelButton);
		cancelButton.addActionListener(this);
	}
	
	public void actionPerformed(ActionEvent event) {
		if (event.getSource() == OKButton) {
			OKButton_Clicked();
		}
		if (event.getSource() == cancelButton) {
			cancelButton_Clicked();
		}
	}
	
	/*
	 * Luu cac tuy chon
	 */
	public void OKButton_Clicked() {
		getCurrentTheme().setCurrent(false);
		getCurrentLanguage().setCurrent(false);
		themes.get(themesComboBox.getSelectedIndex()).setCurrent(true);
		languages.get(languagesComboBox.getSelectedIndex()).setCurrent(true);
		options.save(languages, themes);
		optionDialog.setVisible(false);
		JOptionPane.showMessageDialog(null, lang.getString("OptionDialog.message.restartRequire")); 
	}
	
	public void cancelButton_Clicked() {
		optionDialog.setVisible(false);
	}
}
